import {createStore} from 'vuex'
import {CHANGE_INFO} from './mutation_types'
import homeModule from './modules/home'
import counterModule from './modules/counter'
const store=createStore({
    state:()=>({
        counter:113,
        name:'why',
        level:54,
        avatarURL:'http://xxx',
        users:[
            {id:111,name:'why',age:31},
            {id:112,name:'kobe',age:42},
            {id:113,name:'ding',age:28},
        ],
        rootCounter:200
    }),
    getters:{
        doubleCounter(state){
            return state.counter*2
        },
        totalAge(state){
            return state.users.reduce((preVal,item)=>{
                return preVal+=item.age
            },0)
        },
        message(state,getters){
            return `name:${state.name} level:${state.level} totalAge:${getters.totalAge}`
        },
        getUesrById(state){
            return id=>{
                return state.users.find(item=>item.id===id)
            }
        }
    },
    mutations:{

        increment(state){
            state.counter++
            console.log("increment-state:",state.counter)
        },
        changeName(state,payload){
            state.name=payload
        },
        incrementLevel(state){
            state.level++
        },
        [CHANGE_INFO](state,payload){
            state.name=payload.name
            state.level=payload.level
        }
    },
    actions:{
        incrementAction(context){
            context.commit('increment')
        },
        changeNameAction(context,payload){
            context.commit('changeName',payload)
        },

    },
    modules:{
        home:homeModule,
        counter:counterModule
    }
})

export default store